//
//  schoolTimetableViewController.m
//  Yu01
//
//  Created by 蒋松涵 on 16/8/25.
//  Copyright © 2016年 蒋松涵. All rights reserved.
//

#import "schoolTimetableViewController.h"
#import "RiKeBiaoCell.h"
#import "ZhouViewController.h"
#import "zySheetPickerView.h"

#import "cyhCalenbarview.h"
#import "monthCalenbarview.h"

#import "ClassScheduleModel.h"
#import "RoomInfoModel.h"
#import "LoserAttendanceVC.h"

@interface schoolTimetableViewController ()<UITableViewDataSource,UITableViewDelegate,getdataString,monthdataString,RikeBiaoCellDelegate>

@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)UIScrollView * scrollView;
@property(nonatomic,strong)UITableView * tableView2;
@property (nonatomic, strong)UILabel * Labels;
@property (nonatomic, strong)NSString * NSStringLabel;


@property (nonatomic , strong)cyhCalenbarview * cyhcalenbar;
@property (nonatomic , strong)monthCalenbarview * monthCalenbar;
@property (nonatomic , copy)NSString * daydatestr;


@property (nonatomic,retain) NSString *urlStr;
@property (nonatomic,retain) NSDictionary *urlParameters;

@property (nonatomic,retain) NSString *urlStrs;
@property (nonatomic,retain) NSDictionary *urlParameterss;


@property (nonatomic , strong)NSString * buildingName;//楼号
@property (nonatomic, strong)NSString * createTime;//

@property (nonatomic,strong)NSMutableArray * dataArray;
@property (nonatomic,strong)NSMutableArray * dataArray2;

@end

@implementation schoolTimetableViewController

//- (NSMutableArray *)dataArray {
//    
//    if (!_dataArray) {
//        
//        _dataArray = [[NSMutableArray alloc]init];
//    }
//    return _dataArray;
//    
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"日课表";
    UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"天" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtn)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    _dataArray = [[NSMutableArray alloc]init];
    _dataArray2 = [[NSMutableArray alloc]init];
    
    _urlStr = @"http://www.100xsys.com.cn/school/app/coursePlanInfo/listJson";
    _urlParameters = [[NSDictionary alloc]initWithObjectsAndKeys:@"",@"dayTimes",@"5",@"studentInfoId",@"1",@"appWebConfigId", nil];
    
    //异步执行代码
    [self fecthDataFromNet:_urlStr parameters:_urlParameters];
    
    
    [self iniButton];
    [self tableview];
    //添加日历
    [self riLi];

    [self tableview2];
    
}
//请求数据
- (void)fecthDataFromNet:(NSString *)url parameters:(NSDictionary *)parameter {
    
    NSLog(@"%@",url);
    NSLog(@"%@",parameter);
    //初始化请求工具
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        //        NSLog(@"登录-%@",dic[@"message"]);
        NSLog(@"+++-%@",dic);
        NSMutableArray *List = dic[@"rows"];
        
        for (NSDictionary *dicList in List) {
            
            RoomInfoModel *info1 = [[RoomInfoModel alloc]init];
            
            [info1 setValuesForKeysWithDictionary:dicList];
            [self.dataArray addObject:info1];
            [self.dataArray2 addObject:info1];
            NSLog(@"%ld",self.dataArray.count);
            
            self.latitude = [NSString stringWithFormat:@"%ld",info1.latitude];
            self.longitude = [NSString stringWithFormat:@"%ld",info1.longitude];
            self.createTime = [NSString stringWithFormat:@"%ld",info1.createTime];
            
        }
        
        NSLog(@"$$$$$$%@",self.dataArray.firstObject);
        [self.tableView reloadData];
        [self.tableView2 reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        
    }];
}


#pragma mark - tableview
-(void)tableview
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 120, 80, ScreenHeight - 110) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 80;
//    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    
}


#pragma mark - tableview2
-(void)tableview2
{
    self.tableView2 = [[UITableView alloc]initWithFrame:CGRectMake(80, 150, ScreenWidth - 80, ScreenHeight - 150) style:UITableViewStylePlain];
    self.tableView2.delegate = self;
    self.tableView2.dataSource = self;
//    self.tableView2.scrollEnabled = NO;
    self.tableView2.rowHeight = 80;
    [self.view addSubview:self.tableView2];
    
    [self.tableView2 registerNib:[UINib nibWithNibName:@"RiKeBiaoCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

-(void)iniButton
{
    UIView * Vi_Btn = [[UIView alloc]initWithFrame:CGRectMake(0, 64, 80, 120)];
    Vi_Btn.backgroundColor = [UIColor colorWithRed:236/255.0 green:237/255.0 blue:238/255.0 alpha:1];
    [self.view addSubview:Vi_Btn];
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(30,40, 20, 20);
    [button setBackgroundImage:[UIImage imageNamed:@"下拉按钮图标-07"] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(buttonWithTypess) forControlEvents:UIControlEventTouchUpInside];
    [Vi_Btn addSubview:button];
    
    UIButton * button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(30,10, 20, 20);
    [button2 setBackgroundImage:[UIImage imageNamed:@"下拉按钮图标-08"] forState:UIControlStateNormal];
//    [button2 addTarget:self action:@selector(buttonWithTypess) forControlEvents:UIControlEventTouchUpInside];
    [Vi_Btn addSubview:button2];
    
//    UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 80, 30)];
//    lab.text = self.NSStringLabel;
//    [Vi_Btn addSubview:lab];
}

#pragma mark - 日历
-(void)riLi
{
    //初始化，如果日历小于120.0的高度将默认为120.0高度
    _cyhcalenbar = [[cyhCalenbarview alloc] initD_calenbarframe:CGRectMake(80, 64, ScreenWidth - 80, 90)];
    _cyhcalenbar.daydelegate = self;
    //注册使用
    [_cyhcalenbar daycalenbarviewNSdate];
    
    CGFloat R_1 = 241.0/255.0;
    CGFloat G_1 = 241.0/255.0;
    CGFloat B_1 = 241.0/255.0;
    //日历背景颜色
    _cyhcalenbar.calenbarBGcolor = [UIColor colorWithRed:R_1 green:G_1 blue:B_1 alpha:1];
    
    CGFloat R_2 = 241.0/255.0;
    CGFloat G_2 = 241.0/255.0;
    CGFloat B_2 = 241.0/255.0;
    //星期排列背景颜色
    _cyhcalenbar.WeekBGcolor = [UIColor colorWithRed:R_2 green:G_2 blue:B_2 alpha:1];
    
    CGFloat R_3 = 235.0/255.0;
    CGFloat G_3 = 104.0/255.0;
    CGFloat B_3 = 89.0/255.0;
    //当天日期圆点颜色
    _cyhcalenbar.todayNumBGcolor = [UIColor colorWithRed:R_3 green:G_3 blue:B_3 alpha:1];
    
    CGFloat R_4 = 52.0/255.0;
    CGFloat G_4 = 141.0/255.0;
    CGFloat B_4 = 170.0/255.0;
    //被选中的日期圆点颜色
    _cyhcalenbar.SelecNumBGcolor = [UIColor colorWithRed:R_4 green:G_4 blue:B_4 alpha:1];
    
    CGFloat R_5 = 235.0/255.0;
    CGFloat G_5 = 104.0/255.0;
    CGFloat B_5 = 89.0/255.0;
    //当天日期数字颜色
    _cyhcalenbar.todaytextColor = [UIColor colorWithRed:R_5 green:G_5 blue:B_5 alpha:1];
    
    [self.view addSubview:_cyhcalenbar.daycalenbarview];
}

- (void)getDatestring:(NSString *)datestring
{
    _daydatestr = datestring;
    NSLog(@"回调回来的日期:%@",_daydatestr);
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:self.tableView])
    {
        return self.dataArray2.count;
//        return 10;
    }
    else if ([tableView isEqual:self.tableView2])
    {
        NSLog(@"self.dataArray.count  %ld",self.dataArray.count);
        return self.dataArray.count;
        
//        return 5;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RiKeBiaoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.delegate = self;
    if (!cell)
    {
        cell = [[RiKeBiaoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if ([tableView isEqual:self.tableView])
    {
//        CGFloat h = 736 / ScreenHeight;
//        CGFloat w = 414 / ScreenWidth;
        
        
        RoomInfoModel * roomM = self.dataArray2[indexPath.row];
        
        cell.backgroundColor = [UIColor colorWithRed:236/255.0 green:237/255.0 blue:238/255.0 alpha:1];
        cell.textLabel.text = [NSString stringWithFormat:@"%@",roomM.id];
        
        
        return cell;
    }else     {

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        RoomInfoModel * roomM = self.dataArray[indexPath.row];
        NSLog(@"~~~%@",roomM);
        cell.nameslabel.text = roomM.courseName;
        cell.timeslabel.text = [NSString stringWithFormat:@"%@---%@",roomM.startTime,roomM.endTime];
        cell.roomslabel.text =  [NSString stringWithFormat:@"%@ %@",roomM.buildingName,roomM.classroomNumber];
        cell.classLabel.text = roomM.classInfoIds;
        cell.TeacherLabel.text = roomM.teacherName;

        return cell;
    }
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
}

-(void)buttonWithTypess
{
    NSArray * str  = @[@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月"];
    zySheetPickerView *pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:str andHeadTitle:@"选择月份" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
        
        self.NSStringLabel = choiceString;
        
        [pickerView dismissPicker];
    }];
    [pickerView show];
    
    
}

-(void)rightBtn
{
    self.hidesBottomBarWhenPushed = YES;
    ZhouViewController * ZhouVC = [[ZhouViewController alloc]init];
    [self.navigationController pushViewController:ZhouVC animated:YES];
}


- (void)jump {
    
//    self.hidesBottomBarWhenPushed = YES;
//    LoserAttendanceVC *loser = [[LoserAttendanceVC alloc]init];
//    
//    loser.QianDaoURL = [NSString stringWithFormat:@"http://www.100xsys.com.cn/school/app/ajax_wifiLocationStudyInfo/add?id=%ld",(long)self.IDS];
//    loser.coursePlanInfoId = self.IDS;
//    NSLog(@"+++++%ld",self.IDS);
//    
//    [self.navigationController pushViewController:loser animated:nil];
    
    
    
    
    _urlStrs = @"http://www.100xsys.com.cn/school/app/ajax_wifiLocationStudyInfo/add";
    _urlParameterss = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"wifiLocationStudyInfo.signType",@"1",@"wifiLocationStudyInfo.coursePlanInfoId",self.createTime,@"courseTime",@"1",@"wifiLocationStudyInfo.courseNumber", @"xuesheng1",@"wifiLocationStudyInfo.loginName",@"0",@"wifiList",self.latitude,@"wifiLocationStudyInfo.lat",self.longitude,@"wifiLocationStudyInfo.lon",@"",@"wifiLocationStudyInfo.accuracy",@"",@"wifiLocationStudyInfo.hardwareSerial",@"",@"wifiLocationStudyInfo.remark",@"1",@"appWebConfigId",nil];
    
    
    //异步执行代码
    [self fecthDataFromNet_:_urlStrs parameters_:_urlParameterss];
    
    
}
//请求数据
- (void)fecthDataFromNet_:(NSString *)url parameters_:(NSDictionary *)parameter {
    
    NSLog(@"%@",url);
    NSLog(@"%@",parameter);
    //初始化请求工具
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        //        NSLog(@"登录-%@",dic[@"message"]);
        NSLog(@"+++-%@",dic[@"message"]);
        
        
//        NSMutableArray *List = dic[@"rows"];
//        
//        for (NSDictionary *dicList in List) {
//            
//            RoomInfoModel *info1 = [[RoomInfoModel alloc]init];
//            
//            [info1 setValuesForKeysWithDictionary:dicList];
//            [self.dataArray addObject:info1];
//            [self.dataArray2 addObject:info1];
//            NSLog(@"%ld",self.dataArray.count);
//            
//        }
//        
//        NSLog(@"$$$$$$%@",self.dataArray.firstObject);
//        [self.tableView reloadData];
//        [self.tableView2 reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        
    }];
}

//MARK: - 一个方法就能搞定 右边滑动时跟左边的联动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 如果是 左侧的 tableView 直接return
    if (scrollView == self.tableView) return;
    
    // 取出显示在 视图 且最靠上 的 cell 的 indexPath
    NSIndexPath *topHeaderViewIndexpath = [[self.tableView2 indexPathsForVisibleRows] firstObject];
    
    // 左侧 talbelView 移动到的位置 indexPath
    NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0];
    
    // 移动 左侧 tableView 到 指定 indexPath 居中显示
    [self.tableView selectRowAtIndexPath:moveToIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
